/**
 * Title: AddArticleActivity.java
 * Description:
 * author: kotomifi
 * date: Jul 18, 2014
 * ©2014 apping 社区
 */
package com.apping.demo.bbs;

import com.alibaba.fastjson.JSON;
import com.apping.demo.bbs.bean.PostNews;
import com.apping.demo.bbs.utils.HttpTools;
import com.apping.demo.bbs.utils.SharedPreferencesUtil;
import com.apping.demo.bbs.utils.SignatureUtil;
import com.sina.weibo.sdk.demo.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 *
 */
public class AddArticleActivity extends Activity {

	private long uid;
	private String title;
	private String content;
	private String author;
	private String signature;
	
	private EditText addArticleTitle;
	private EditText addArticleContent;
	private Button addArticleBtn;
	private PostNews postNews;
	private static final int POST_ARTICLE_SUCCESS = 0;
	private static final int POST_ARTICLE_FAIL = 1;
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case POST_ARTICLE_SUCCESS:
				Toast.makeText(AddArticleActivity.this,
						"添加成功", 2000).show();
				break;
			case POST_ARTICLE_FAIL:
				Toast.makeText(AddArticleActivity.this,
						"添加失败", 2000).show();
				break;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_article);
		
		initUI();
	}
	
	/**
	 * 提交文章
	 */
	public void initUI() {
		addArticleTitle = (EditText)findViewById(R.id.add_article_title);
		addArticleContent = (EditText)findViewById(R.id.add_article_content);
		addArticleBtn = (Button)findViewById(R.id.add_article_btn);
		
		addArticleBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				SharedPreferencesUtil preferences = 
						new SharedPreferencesUtil(getApplication());
				//long uid = preferences.readLong("com_apping_demo_bbs", "uid");
//				String ticket = preferences.readString("com_apping_demo_bbs", "ticket");
				title = addArticleTitle.getText().toString();
				content = addArticleContent.getText().toString();
				if (title.equals("")) {
					Toast.makeText(AddArticleActivity.this, 
							"请填写文章标题", 2000).show();
				} else if (content.equals("")) {
					Toast.makeText(AddArticleActivity.this, 
							"请输入文章内容", 2000).show();
				} else {
					///////////////////////////////////////////////////
					uid = 1;
					author = "nimei";
					String ticket = "bd637611abfe504f1ad40547af91687f";
					signature = SignatureUtil.getSignature(
					title, 
					content, 
					String.valueOf(uid), 
					ticket, 
					author);
					///////////////////////////////////////////////////////
					new Thread() {
						@Override
						public void run() {
							// 发送post请求
							String path = "http://42.96.192.37:8080/apping/demo/postnews";
							
							String param = "";
							param = "uid=" + uid +
									"&signature=" + signature +
									"&title=" + title +
									"&content=" + content + 
									"&author=" + author;
							byte[] data = param.getBytes();
							HttpTools httpTools = new HttpTools();
							httpTools.post(path, data);
							String responJson = httpTools.getContent();
							if (JSON.parseObject(responJson).getString("rst").equals("SUCCESS")) {
								handler.sendEmptyMessage(POST_ARTICLE_SUCCESS);
								finish();
							} else {
								handler.sendEmptyMessage(POST_ARTICLE_FAIL);
								finish();
							}
							
						}
					}.start();
				}
			}
		});
	}
}
